package com.lovo.project.service.impl;

import com.lovo.project.dao.ProductDAO;
import com.lovo.project.enums.ProductEnum;
import com.lovo.project.exception.OurException;
import com.lovo.project.model.ProductDO;
import com.lovo.project.service.ProductService;
import com.lovo.project.util.MyBatisUtil;
import com.sun.org.apache.bcel.internal.generic.NEW;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * @title  商品业务实现类
 * @author: chenqiang
 * @date: 2020/11/4 10:40
 */
public class ProductServiceImpl implements ProductService {

    @Override
    public void addProduct(ProductDO productDO) {
        SqlSession sqlSession  = MyBatisUtil.getSqlSession();
        ProductDAO dao = sqlSession.getMapper(ProductDAO.class);
         int num =  dao.addProduct(productDO);

        try {
            if(num<=0){
                throw new OurException(ProductEnum.PRODUCT_ADD_ENUM);
            }
            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            sqlSession.close();
        }


    }

    @Override
    public void updateProduct(Integer id, Integer productMoney, String productExclusiveSpecification) {
        SqlSession sqlSession  = MyBatisUtil.getSqlSession();
        ProductDAO dao = sqlSession.getMapper(ProductDAO.class);
        int num =  dao.updateProduct(id,productMoney,productExclusiveSpecification);

        try {
            if(num<=0){
                throw new OurException(ProductEnum.PRODUCT_Update_ENUM);
            }
            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            sqlSession.close();
        }
    }

    @Override
    public ProductDO findById(Integer id) {
        SqlSession sqlSession  = MyBatisUtil.getSqlSession();
        ProductDAO dao = sqlSession.getMapper(ProductDAO.class);
        ProductDO productDO = dao.findById(id);

        try {
            if(productDO == null){
                throw new OurException(ProductEnum.PRODUCT_Find_ENUM);
            }
            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            sqlSession.close();
        }
        return productDO;
    }

    @Override
    public List<ProductDO> findAllByProductName(Integer id,String productName) {
        SqlSession sqlSession  = MyBatisUtil.getSqlSession();
        ProductDAO dao = sqlSession.getMapper(ProductDAO.class);
        List<ProductDO> list =  dao.findAllByProductName(id,productName);

        try {
            if(list.size()<=0){
                throw new OurException(ProductEnum.PRODUCT_Find_ENUM);
            }
            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            sqlSession.close();
        }
        return list;
    }

    @Override
    public void UpdateProductByIsDelete(Integer id) {
        SqlSession sqlSession  = MyBatisUtil.getSqlSession();
        ProductDAO dao = sqlSession.getMapper(ProductDAO.class);
         int num  = dao.UpdateProductByIsDelete(id);

        try {
            if(num<=0){
                throw new OurException(ProductEnum.PRODUCT_DELETE_ENUM);
            }
            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            sqlSession.close();
        }

    }
}
